package com.movitech.mobile.ddzy.modules.sys.service.impl;

import java.util.List;

import com.movitech.mobile.cz.base.common.util.HeadersUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.movitech.mobile.cz.base.common.util.StringUtils;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.sessionuser.SessionButtonService;
import com.movitech.mobile.cz.base.sessionuser.SessionButtonVo;
import com.movitech.mobile.cz.base.sessionuser.SessionUserVo;
import com.movitech.mobile.cz.base.util.UserUtils;
import com.movitech.mobile.ddzy.bpm.feignclient.MbsDataAuthClient;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Service("extSessionButtonService")
public class SessionButtonServiceImpl implements SessionButtonService {

    @Value("${api.feign.auth.appuuid}")
    private String apiFeignAuthAppuid;

    @Autowired
    MbsDataAuthClient mbsDataAuthClient;

    @Override
    public List<SessionButtonVo> getSessionButton() {
        System.out.println("=====getButton=======方法");
        Gson gson = new Gson();
        List<SessionButtonVo> lstSessionButtonVo = null;
        SessionUserVo sessionUserVo = UserUtils.getCurrentUserVo();
        System.out.println("====getButton====sessionUserId===" + sessionUserVo.getEmployeeID());
        if (sessionUserVo != null) {
            String employeeId = sessionUserVo.getEmployeeID();
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = requestAttributes.getRequest();
            String token = HeadersUtils.getHeadByKey(request, "token");
            System.out.println("----token===" + token);
            String returnJson = mbsDataAuthClient.getAuthButton(apiFeignAuthAppuid, token);//HttpUtil.sendGet(mbsAuthUrl+employeeId);
            System.out.println("=====getButton=======" + returnJson);
            if (StringUtils.isNotEmpty(returnJson)) {
                RestReturnResult<List<SessionButtonVo>> result = gson.fromJson(returnJson, new TypeToken<RestReturnResult<List<SessionButtonVo>>>() {
                }.getType());
                if (result != null) {
                    lstSessionButtonVo = result.getData();
                }
            }
        }
        return lstSessionButtonVo;
    }

}
